home *** CD-ROM | disk | FTP | other *** search
/ Total Network Tools 2002 / NextStepPublishing-TotalNetworkTools2002-Win95.iso / Archive / Misc Servers / Zope.exe / CREOSOTE.PY < prev    next >
Encoding:
Python Source  |  2000-09-07  |  2.1 KB  |  67 lines

  1. #!/usr/bin/env python
  2. # creosote.py - lightweight message passing with datagrams
  3. # JeffBauer@bigfoot.com
  4.  
  5. import sys, socket
  6.  
  7. BUFSIZE = 1024
  8. PORT = 7739
  9.  
  10. def spew(msg, host='localhost', port=PORT):
  11.     s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  12.     s.bind(('', 0))
  13.     s.sendto(msg, (host, port))
  14.  
  15. def bucket(port=PORT, logfile=None):
  16.     s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  17.     s.bind(('', port))
  18.     print 'creosote bucket waiting on port: %s' % port
  19.     if logfile:
  20.         f = open(logfile, 'a+')
  21.     while 1:
  22.         data, addr = s.recvfrom(BUFSIZE)
  23.         print `data`[1:-1]
  24.         if logfile:
  25.             f.write(`data`[1:-1]+'\n')
  26.             f.flush()
  27.  
  28. class MrCreosote:
  29.     """lightweight message passing with datagrams"""
  30.     def __init__(self, host='localhost', port=PORT):
  31.         self.host = host
  32.         self.port = port
  33.         self.client = None
  34.         self.disabled = 0
  35.         self.redirect_server = None
  36.         self.redirect_client = None
  37.     def bucket(self, logfile=None):
  38.         bucket(self.port, logfile)
  39.     def redirector(self, host, port=PORT):
  40.         if self.disabled:
  41.             return
  42.         if self.redirect_server == None:
  43.             self.redirect_server = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  44.             self.redirect_server.bind(('', self.port))
  45.         if self.redirect_client == None:
  46.             self.redirect_client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  47.             self.redirect_client.bind(('', 0))
  48.         while 1:
  49.             data, addr = self.redirect_server.recvfrom(BUFSIZE)
  50.             self.redirect_client.sendto(data, (host, port))
  51.     def spew(self, msg):
  52.         if self.disabled:
  53.             return
  54.         if self.client == None:
  55.             self.client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  56.             self.client.bind(('', 0))
  57.         self.client.sendto(msg, (self.host, self.port))
  58.  
  59. if __name__ == '__main__':
  60.     """usage: creosote [message]"""
  61.     if len(sys.argv) < 2:
  62.         bucket()
  63.     else:
  64.         from string import joinfields
  65.         spew(joinfields(sys.argv[1:],' '))
  66.  
  67.